import {page} from "../common/PageBuilder";
import {BasePage, BasePageData} from "../common/core/BasePage";
import {UserPage} from "../common/partPages/UserPage";
import {DataLoader, DataOccasion, field, occasion} from "../../modules/coreModule/data/DataLoader";
import {BaseData, PK, pkType} from "../../modules/coreModule/data/BaseData";
import {DateUtils} from "../../utils/DateUtils";
import { LongTimeTime } from "../../modules/userModule/data/User";
import {UserInterface} from "../../modules/userModule/UserCommon";
import {waitForLogin} from "../../modules/userModule/managers/UserManager";
import {waitForDataLoad} from "../../modules/coreModule/managers/DataManager";
import {TaskInterface} from "../../modules/taskModule/TaskCommon";
import {TaskIndexData} from "../../modules/taskModule/data/Task";
import {QueryPage} from "../common/partPages/QueryPage";
import {ShareAppPage} from "../common/partPages/SharePage";

export enum InviteStatus {
	Pending = -1, Success, Failed
}

class UserInviteData extends BaseData {

	@field(String)
	openid: string;
	@field(String)
	nickName: string;
	@field(String)
	displayName: string;
	@field(String)
	avatarUrl: string;
	@field(Number)
	createdAt: number;
	@field(Number)
	status: InviteStatus;

	// region 额外数据

	@field(String)
	@occasion(DataOccasion.Extra)
	createTimeStr: string;
	@field(String)
	@occasion(DataOccasion.Extra)
	statusStr: string;

	public refresh() {
		this.createTimeStr = DateUtils.time2Str(this.createdAt);
		this.statusStr = ["邀请中", "已成功", "已失败"][this.status + 1];
	}

	// endregion

	public static testData(cnt = 5) {
		const res = [];
		for (let i = 0; i < cnt; i++) {
			const data = new UserInviteData();
			data.nickName = `测试用户${i}`;
			data.displayName = `显示名称${i}`;
			data.avatarUrl = "../../assets/commission/MRB.jpg";
			data.createdAt = Date.now() - Math.random() * 100000000 * i;
			data.status = Math.floor(Math.random() * 3) - 1;
			res.push(data);
		}
		return res;
	}
}

class Data extends BasePageData {

	@field([UserInviteData])
	list: UserInviteData[] = [];
}

@page("commission", "推广分成")
export class CommissionPage extends BasePage<Data> {

	public data = new Data();

	/**
	 * 部分页
	 */
	public userPage = new UserPage();
	public shareAppPage = new ShareAppPage();
	public queryPage =
		new QueryPage(this.loadData.bind(this));

	onLoad(e) {
		super.onLoad(e);
		this.queryPage.refresh();
	}

	/**
	 * 数据回调
	 */
	@waitForLogin
	public async loadData(queryParams) {
		const res = await UserInterface.GetInvitees({
			...queryParams
		})
		const list = res.invitees?.map(
			t => DataLoader.load(UserInviteData, t));
		return list && this.setData({list});
	}

}

